/**
 * JAVACC DEMO 1.0
 */
package com.apache.uct.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.tools.ClassToolsUtil;
import com.apache.uct.common.entity.Dept;
import com.apache.uct.manager.DeptManager;
import com.apache.uct.util.UctStringUtil;
import org.apache.log4j.Logger;

import java.util.List;

/**
 * description:  获取部门列表插件
 *
 * @author iussoft 创建时间：2016-3-26
 */
public class DeptListPlugin implements PluginConnector {

    private Logger log = Logger.getLogger(DeptListPlugin.class);

    private IDao deptDao;

    private DeptManager deptManager;

    /**
     * TODO 获取部门列表插件.  <br>
     * 参数说明:<br>
     * dept属性:按部门属性查询部门列表;<br>
     * 分页设置<br>
     * pageSize:每页记录数
     * pageIndex:当前页码
     */
    public Object execute(ParamsVo vo) throws Exception {
        String orderBy = "create_time";
        ResultEntity result = new ResultEntity();
        ParamsVo<Dept> deptVo = new ParamsVo<Dept>();

        Dept dept = new Dept();
        dept = (Dept) ClassToolsUtil.getInstance().convert2Bean(vo.getParams(), dept);
        deptVo.setObj(dept);
        try {
            result.setResult("true");
            MethodParam param = setMethodParams(vo, 2, orderBy);
            if (Validator.isEmpty(param)) {
                result.setMessage("查询条件为空或不存在相关信息");
                return result;
            }
            int pageSize = Integer.valueOf(Validator
                    .getDefaultStr(String.valueOf(vo.getParams("pageSize")),
                            Validator.getDefaultStr(String.valueOf(vo.getParams("rows")), "0")));
            int pageIndex = Integer.valueOf(Validator
                    .getDefaultStr(String.valueOf(vo.getParams("pageIndex")),
                            Validator.getDefaultStr(String.valueOf(vo.getParams("page")), "0")));
            if (pageSize == 0 && pageIndex == 0) {
                List<Dept> list = deptManager.getList(deptVo);
                result.setEntity(list);
            } else {
                param.setPageIndex(pageIndex);
                param.setPageSize(pageSize);
                param.setOrderby(orderBy + " desc");
                Page page = deptDao.pageSelect(param);
                result.setEntity(page);
            }
        } catch (Exception e) {
            result.setResult("false");
            log.error("执行机构查询失败 ", e);
            result.setMessage("机构查询失败！");
        }
        return result;
    }

    private MethodParam setMethodParams(ParamsVo vo, int type, String orderBy) {
        StringBuffer sb = new StringBuffer(deptDao.getSql(type));
        MethodParam param = new MethodParam("ByProperty", "", "", Dept.class.getName());
        String str = buildParams(param, vo);
        if (Validator.isNull(str)) {
            return null;
        }
        sb.append(str);

        if (Validator.isNotNull(orderBy)) {
            sb.append(" order by " + UctStringUtil.toCamelNamed(orderBy) + " desc");
        }
        param.setSqlStr(sb.toString());
        return param;
    }

    private String buildParams(MethodParam param, ParamsVo vo) {
        StringBuffer sb = new StringBuffer();
        if (Validator.isNotNull(String.valueOf(vo.getParams("sysEname")))) {
            sb.append(" and sysEname = :sysEname");
            param.setParams("sysEname", String.valueOf(vo.getParams("sysEname")));
        }
        if (Validator.isNotNull(String.valueOf(vo.getParams("orgId")))) {
            sb.append(" and orgId = :orgId");
            param.setParams("orgId", String.valueOf(vo.getParams("orgId")));
        }
        if (Validator.isNotNull(String.valueOf(vo.getParams("deptId")))) {
            sb.append(" and deptId = :deptId");
            param.setParams("deptId", String.valueOf(vo.getParams("deptId")));
        }
        if (Validator.isNotNull(String.valueOf(vo.getParams("orgEname")))) {
            sb.append(" and orgEname = :orgEname");
            param.setParams("orgEname", String.valueOf(vo.getParams("orgEname")));
        }
        if (Validator.isNotNull(String.valueOf(vo.getParams("deptCode")))) {
            sb.append(" and deptCode = :deptCode");
            param.setParams("deptCode", String.valueOf(vo.getParams("deptCode")));
        }
        if (Validator.isNotNull(String.valueOf(vo.getParams("deptEname")))) {
            sb.append(" and deptEname = :deptEname");
            param.setParams("deptEname", String.valueOf(vo.getParams("deptEname")));
        }
        if (Validator.isNotNull(String.valueOf(vo.getParams("fullEname")))) {
            sb.append(" and fullEname = :fullEname");
            param.setParams("fullEname", String.valueOf(vo.getParams("fullEname")));
        }
        if (Validator.isNotNull(String.valueOf(vo.getParams("fullCname")))) {
            sb.append(" and fullCname = :fullCname");
            param.setParams("fullCname", String.valueOf(vo.getParams("fullCname")));
        }
        if (Validator.isNotNull(String.valueOf(vo.getParams("deptCname")))) {
            sb.append(" and deptCname = :deptCname");
            param.setParams("deptCname", String.valueOf(vo.getParams("deptCname")));
        }
        if (Validator.isNotNull(String.valueOf(vo.getParams("fatherId")))) {
            sb.append(" and fatherId = :fatherId");
            param.setParams("fatherId", String.valueOf(vo.getParams("fatherId")));
        }
        if (Validator.isNotNull(String.valueOf(vo.getParams("deptRemark")))) {
            sb.append(" and deptRemark = :deptRemark");
            param.setParams("deptRemark", String.valueOf(vo.getParams("deptRemark")));
        }
        return sb.toString();
    }

    public void setDeptDao(IDao deptDao) {
        this.deptDao = deptDao;
    }

    public void setDeptManager(DeptManager deptManager) {
        this.deptManager = deptManager;
    }

}
